/**
 * Created by Administrator on 2015/8/21.
 */
var async = require('async');
//首页有关的模型
var JuiceHomePageM = require('../models/juiceHome');
var ClzM = require('../models/classify');
var ProM = require('../models/product');
var signDayM = require('../models/signday.js');

module.exports = {
    /**
     * 显示首页数据
     * @param req
     * @param res
     * @param next
     */
    getHomepage : function(req, res){
        var uid = req.AV.user.id;
        async.parallel([
                function(callback){
                    JuiceHomePageM.getHomepage().then(function(data){
                        var lbt = []; //轮播图
                        var len = data.length;
                        for(var i = 0 ; i < len ; i++){
                            var d = data[i];
                            switch(d.get('cName')){
                                case 'lbt':
                                    lbt.push(d);
                                    break;
                            }
                        }
                        callback(null,{lbt:lbt});
                    });
                },
                function(callback){
                    JuiceHomePageM.getHomeClassify(["grgzwm","grxgqwm","grtpwm","grxls"]).then(function(data){
                        async.mapSeries(data,function(item,next){
                            ProM.listByClassifyId(item.id,1,4).then(function(list){
                                //console.log(list);
                                next(null,{'title':item.get('title'),id:item.id,list:list});
                            });
                        },function(err,res){
                            if(err){
                                callback(err);
                            }
                            callback(null,res);
                        });

                    }).catch(function(result){
                    });
                },

            ]
            ,function(err,results){
                if(err){
                    //console.log(err);
                    return;
                }
                res.render('juiceHome/index',{title:'果然100',lbt:results[0].lbt,classify:results[1]});
            });
    },
    /**
     * 需要在url中传入分类的类目名称和页码
     * cname
     * page
     * @param req
     * @param res
     * @param next
     */
    listByClass:function(req,res,next){
        //获取分类的简码
        var cname = req.params.cname;
        //获取页码
        var page = req.params.p;

        async.waterfall([

            //通过cname获取cid

            function(callback){
                ClzM.getByName(cname).then(function(clz){
                    callback(null,clz);
                });
            },
            //通过cid获取商品列表
            //clz 指向前面一个函数的输出值
            function(clz,callback){
                ProM.list(clz.id,page).then(function(list){
                    callback(null,{classify:clz,list:list});
                }).catch(function(err){
                    //TODO:错误的处理
                });
            }
        ],function(err,result){
            if(err){
                //TODO:
                return;
            }
            //res.send('111');//直接把内容输出给浏览器

            res.render('p/list',{title:result.classify.get('intro'),list:result.list});
        });
    },
    /**
     *
     * @param req
     * @param res
     */


    packageIndex : function(req, res){
        var uid = req.AV.user.id;
        async.parallel([
                function(callback){
                    JuiceHomePageM.showPackage().then(function(data){
                        var lbt = []; //轮播图
                        var len = data.length;
                        for(var i = 0 ; i < len ; i++){
                            var d = data[i];
                            var pid = d.id;
                            lbt.push([pid,JSON.parse(d.get('picurlarray'))[0]]);
                        }
                        callback(null,{lbt:lbt});
                    });
                },
                function(callback){
                    JuiceHomePageM.getHomeClassify(["grgzwm","grxgqwm","grtpwm","grxls"]).then(function(data){
                        async.mapSeries(data,function(item,next){
                            ProM.listByClassifyIdXWC(item.id,1,100).then(function(list){
                                //console.log(list);
                                next(null,{'title':item.get('title'),id:item.id,list:list});
                            });
                        },function(err,res){
                            if(err){
                                callback(err);
                            }
                            callback(null,res);
                        });

                    }).catch(function(result){
                    });
                },
                function(callback){
                    //清理购物车
                    JuiceHomePageM.setProductCheck(uid).then(function(data){
                        callback(null, data)
                    }).catch(function(err){
                        callback(err)
                    })
                },
            ]
            ,function(err,results){
                if(err){
                    //console.log(err);
                    return;
                }
                res.render('juiceHome/juicePackage',{title:'果然100',lbt:results[0].lbt,classify:results[1]});
            });

    },

    /**
     * 根据uid查询当前用户套餐商品列表
     */
    getPackageList:function(req,res,next){
        var uid = req.AV.user.id;
        async.waterfall([
            //根据uid查询用户套餐记录

            function(callback){
                if(uid){
                    JuiceHomePageM.showMyPackage(uid).then(function(packageList){
                        callback(null,packageList);
                    }).catch(function(err){
                        callback(err);
                    });
                }else{
                    callback({code:-1});
                }
            },
            //根据套餐记录查询商品规格列表
            function(packageList,callback){
                var proList = new Array();
                async.eachSeries(packageList,function(eachInfo,callback1){
                    ProM.getProDetailByObjId(eachInfo.get('pid')).then(function(pro){
                        proList.push(
                            {
                                pid:eachInfo.get('pid'),
                                cid:pro.get('cid'),
                                name:pro.get('name'),
                                psid:eachInfo.get('psid'),
                                psName:pro.get('spec')[eachInfo.get('psid')].name,
                                psPrice:pro.get('spec')[eachInfo.get('psid')].price, //套餐价
                                psGrprice:pro.get('spec')[eachInfo.get('psid')].grprice,   //原价
                                count:eachInfo.get('count')
                            }
                        );
                        callback1(null,proList);
                    });
                },function(){
                    callback(null,proList);
                });
            },
        ],function(err,result){
            if(err){
                //TODO
                return;
            }else{
                res.json({info:result});
            }
        });
    },
    getProInfo:function(req,res,next){
        var pid = req.query.pid ;
        ProM.getProDetailByObjId(pid).then(function(pro){
            var spec = new Array();
            async.eachOfSeries(pro.get("spec"),function(each,key,callback1){
                spec.push(
                    {
                        psid:key,
                        spec:each
                    }
                );
            callback1(null, spec);
            },function(){
                //console.log(spec);
                res.json({pro:pro,spec:spec});
            });
        });

    },
    saveJuicePro : function(req, res){
        var uid = req.AV.user.id;
        var pid = req.query.pid ;
        var psid = req.query.psid ;
        var count = req.query.count ;
        JuiceHomePageM.addIntoPackage(uid,pid,psid,count).then(function(data){
            if(data.code === 0 ){
                //保存成功
                //console.log('success');
                res.json({code:0});
            }else{
                res.json({code:1});
            }
        });
    },
    deleteJuicePro:function(req,res){
        var uid = req.AV.user.id;
        var pid = req.query.pid ;
        var psid = req.query.psid ;
        JuiceHomePageM.deleteFromPackage(uid,pid,psid).then(function(data){
            if(data.code === 0 ){
                //保存成功
                res.json({code:0});
            }else{
                res.json({code:1});
            }
        });
    }
};
